﻿using System;
using System.Collections.Generic;

public static class LocalizationManager
{
    // 存储所有语言的翻译
    // 结构: <语言代码, <文本键, 翻译文本>>
    private static Dictionary<string, Dictionary<string, string>> _languages = new Dictionary<string, Dictionary<string, string>>();

    // 当前语言
    public static string CurrentLanguage { get; private set; } = "en-US"; // 默认简体中文

    // 当语言改变时触发的事件
    public static event Action LanguageChanged;

    // 初始化，加载所有语言
    public static void Initialize()
    {
        // --- English ---
        var en = new Dictionary<string, string>
        {
            { "WindowTitle", "MMD MorphSettings Generate" },
            { "Error_NotPmdFile", "Not a valid PMD file." },
            { "Tooltip_Topmost", "Pin Window" },
            { "Prompt_LoadModel", "Please load a model file first." },
            { "Title_Prompt", "Prompt" },
            { "Title_Success", "Success" },
            { "Success_ExportMessage", "Configuration file generated successfully!\n1. ModelList.ini has been updated.\n2. Profile generated: {0}.ini" },
            { "Title_Error", "Error" },
            { "Error_ExportFailed", "Export failed: {0}" },
            { "Prompt_SelectPathAndFiles", "Please select an export path and add files first." },
            { "Error_CannotParse", "Cannot parse: {0}" },
            { "Error_ProcessingFailed", "Error processing {0}: {1}" },
            { "Title_BatchResult", "Batch Processing Result" },
            { "BatchSummary", "Batch processing complete!\nSuccess: {0}\nFailed: {1}" },
            { "BatchSummary_Details", "\n\nError Details:\n{0}" },
            { "Error_FileLoadFailed", "File loading failed: {0}, please try converting to PMX file and import again." },
            { "importFileText_Translation", "Import" },
            { "FilePathButton_Traslation", "Open" },
            { "ModelNameText_Traslation", "Model" },
            { "OutputButton_Traslation", "Export" },
            { "OnlyFileTabPage_Traslation", "Single File" },
            { "BatchFileTabPage_Traslation", "Batch Files" },
            { "BatchOutputText_Traslation", "Export" },
            { "BatchAddFileButton_Traslation", "Add Files" },
            { "BatchDeleteButton_Traslation", "Remove" },
            { "OnlySelectPath", "Please select an output path first" },
            { "SelectSettingPathTip", "Select the folder where you want to export the configuration files" },
            { "Success_ExportMessage_en","Configuration file generated successfully!\n1. ModelList.ini has been updated.\n2. Profile generated： {0}.ini\n\nWarning: The English translation of this model's expression is not complete. If you are using the English version of MMD, It is recommended to add English emoticon names manually in PMX/PMD file and update the name of Morph-IN in {0}. ini." },
            { "BatchWarning_NoEnNames_Header","\n\nWarning: the following model files lack English names for emoticons and may need to be added manually:" }
        };
        _languages["en-US"] = en;

        // --- 简体中文 ---
        var zh_CN = new Dictionary<string, string>
        {
            { "WindowTitle", "MMD表情配置文件生成" },
            { "Error_NotPmdFile", "不是一个有效的PMD文件。" },
            { "Tooltip_Topmost", "窗口置顶" },
            { "Prompt_LoadModel", "请先加载一个模型文件。" },
            { "Title_Prompt", "提示" },
            { "Title_Success", "成功" },
            { "Success_ExportMessage", "配置文件已成功生成！\n1. ModelList.ini 已更新。\n2.已生成配置文件： {0}.ini" },
            { "Title_Error", "错误" },
            { "Error_ExportFailed", "导出失败：{0}" },
            { "Prompt_SelectPathAndFiles", "请先选择导出路径并添加文件。" },
            { "Error_CannotParse", "无法解析: {0}" },
            { "Error_ProcessingFailed", "处理 {0} 时出错: {1}" },
            { "Title_BatchResult", "批量处理结果" },
            { "BatchSummary", "批量处理完成！\n成功: {0}\n失败: {1}" },
            { "BatchSummary_Details", "\n\n错误详情:\n{0}" },
            { "Error_FileLoadFailed", "加载文件失败：{0}，请尝试转换PMX文件再导入。" },
            { "importFileText_Translation", "导入文件" } ,
            { "FilePathButton_Traslation", "浏览" } ,
            { "ModelNameText_Traslation", "模型名" } ,
            { "OutputButton_Traslation", "导出" } ,
            { "OnlyFileTabPage_Traslation", "单文件" } ,
            { "BatchFileTabPage_Traslation", "批量文件" } ,
            { "BatchOutputText_Traslation", "导出路径" } ,
            { "BatchAddFileButton_Traslation", "添加文件" } ,
            { "BatchDeleteButton_Traslation", "删除项" },
            { "OnlySelectPath", "请先选择一个输出路径" },
            { "SelectSettingPathTip", "请选择导出配置文件的文件夹" },
            { "Success_ExportMessage_en","配置文件已成功生成！\n1. ModelList.ini 已更新。\n2. 已生成配置文件： {0}.ini\n警告：此模型的表情英文翻译不完整。如果您使用英文版MMD，建议手动在PMX/PMD文件中添加英文表情名称，并在 {0}.ini 更新 Morph_EN 的名称。" },
            { "BatchWarning_NoEnNames_Header","\n\n警告：下列模型文件缺少表情的英文名称，可能需要手动添加名称：" }
        };
        _languages["zh-CN"] = zh_CN;

        // --- 繁體中文 ---
        var zh_TW = new Dictionary<string, string>
        {
            { "WindowTitle", "MMD表情設定文件生成" },
            { "Error_NotPmdFile", "不是一個有效的PMD檔案。" },
            { "Tooltip_Topmost", "視窗置頂" },
            { "Prompt_LoadModel", "請先載入一個模型檔案。" },
            { "Title_Prompt", "提示" },
            { "Title_Success", "成功" },
            { "Success_ExportMessage", "設定檔已成功生成！\n1. ModelList.ini 已更新。\n2.已生成設定檔： {0}.ini" },
            { "Title_Error", "錯誤" },
            { "Error_ExportFailed", "匯出失敗：{0}" },
            { "Prompt_SelectPathAndFiles", "請先選擇匯出路徑並新增檔案。" },
            { "Error_CannotParse", "無法解析: {0}" },
            { "Error_ProcessingFailed", "處理 {0} 時出錯: {1}" },
            { "Title_BatchResult", "批次處理結果" },
            { "BatchSummary", "批次處理完成！\n成功: {0}\n失敗: {1}" },
            { "BatchSummary_Details", "\n\n錯誤詳情:\n{0}" },
            { "Error_FileLoadFailed", "載入檔案失敗：{0}，請嘗試轉換為PMX檔案再匯入。" },
            { "importFileText_Translation", "匯入檔案" },
            { "FilePathButton_Traslation", "瀏覽" },
            { "ModelNameText_Traslation", "模型名稱" },
            { "OutputButton_Traslation", "匯出" },
            { "OnlyFileTabPage_Traslation", "單一檔案" },
            { "BatchFileTabPage_Traslation", "批次檔案" },
            { "BatchOutputText_Traslation", "匯出路徑" },
            { "BatchAddFileButton_Traslation", "新增檔案" },
            { "BatchDeleteButton_Traslation", "移除選取" },
            { "OnlySelectPath", "請先選擇壹個輸出路徑" },
            { "SelectSettingPathTip", "請選擇導出配置文件的文件夾" },
            { "Success_ExportMessage_en","配置文件已成功生成！\n1. ModelList.ini 已更新。\n2. 已生成配置文件： {0}.ini\n\n警告：此模型的表情英文翻譯不完整。如果您使用英文版MMD，建議手動在PMX/PMD文件中添加英文表情名稱，併在 {0}.ini 更新 Morph_EN 的名稱。" },
            { "BatchWarning_NoEnNames_Header","\n\n警告：下列模型文件缺少表情的英文名稱，可能需要手動添加名稱：" }
        };
        _languages["zh-TW"] = zh_TW;

        // --- 日本語 ---
        var ja_JP = new Dictionary<string, string>
        {
            { "WindowTitle", "MMDモーフ設定ジェネレーター" },
            { "Error_NotPmdFile", "有効なPMDファイルではありません。" },
            { "Tooltip_Topmost", "ウィンドウを最前面に表示" },
            { "Prompt_LoadModel", "まずモデルファイルをロードしてください。" },
            { "Title_Prompt", "プロンプト" },
            { "Title_Success", "成功" },
            { "Success_ExportMessage", "設定ファイルが正常に生成されました！\n1. ModelList.ini が更新されました。\n2.生成された設定ファイル： {0}.ini" },
            { "Title_Error", "エラー" },
            { "Error_ExportFailed", "エクスポートに失敗しました：{0}" },
            { "Prompt_SelectPathAndFiles", "まずエクスポートパスを選択し、ファイルを追加してください。" },
            { "Error_CannotParse", "解析できません: {0}" },
            { "Error_ProcessingFailed", "{0} の処理中にエラーが発生しました: {1}" },
            { "Title_BatchResult", "バッチ処理結果" },
            { "BatchSummary", "バッチ処理が完了しました！\n成功: {0}\n失敗: {1}" },
            { "BatchSummary_Details", "\n\nエラー詳細:\n{0}" },
            { "Error_FileLoadFailed", "ファイルの読み込みに失敗しました：{0}。PMXファイルに変換してから再度インポートしてみてください。" },
            { "importFileText_Translation", "ファイル" },
            { "FilePathButton_Traslation", "参照" },
            { "ModelNameText_Traslation", "モデル名" },
            { "OutputButton_Traslation", "出力" },
            { "OnlyFileTabPage_Traslation", "単一ファイル" },
            { "BatchFileTabPage_Traslation", "バッチファイル" },
            { "BatchOutputText_Traslation", "出力" },
            { "BatchAddFileButton_Traslation", "追加" },
            { "BatchDeleteButton_Traslation", "削除" },
            { "OnlySelectPath", "まず出力パスを選択してください" },
            { "SelectSettingPathTip", "構成ファイルをエクスポートするフォルダを選択してください" },
            { "Success_ExportMessage_en","設定ファイルが正常に生成されました！\n1. ModelList.ini が更新されました。\n2.生成された設定ファイル： {0}.ini\n\n警告：このモデルの表情の英語翻訳は不完全です。MMDの英語版を使用する場合は、PMX/PMDファイルに英語の絵文字名を手動で追加することをお勧めします、Morph_ENの名前を{0}.iniで更新することをお勧めします。" },
            { "BatchWarning_NoEnNames_Header","\n\n警告:次のモデルファイルには表情の英語名がないため、手動で名前を追加する必要がある場合があります:" }
        };
        _languages["ja-JP"] = ja_JP;
    }

    // 设置语言
    public static void SetLanguage(string langCode)
    {
        CurrentLanguage = langCode;
        LanguageChanged?.Invoke(); // 触发事件，通知UI更新
    }

    // 获取翻译后的字符串
    public static string GetString(string key)
    {
        if (_languages.ContainsKey(CurrentLanguage) && _languages[CurrentLanguage].ContainsKey(key))
        {
            return _languages[CurrentLanguage][key];
        }
        // 如果当前语言或键不存在，则回退到英文
        if (_languages["en-US"].ContainsKey(key))
        {
            return _languages["en-US"][key];
        }
        return key; // 最坏情况，返回键本身
    }
}